import os
from PyQt5.QtCore import Qt, QModelIndex, pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QPushButton,\
                            QCheckBox,\
                            QComboBox,\
                            QDialog,\
                            QDialogButtonBox,\
                            QGridLayout,\
                            QLabel,\
                            QLineEdit,\
                            QMessageBox
from PyQt5.QtSql import     QSqlQueryModel


class NewDialog(QDialog):
    newed = pyqtSignal(str, str, str, str, str)

    def __init__(self, parent):
        super().__init__(parent)
        self.setWindowTitle('New Rulebook...')
        self.db = parent.db
        self.rbkmodel = parent.models['rulebooks']
        self.parent = parent
        grid = QGridLayout()
        self.resize(500, 200)

        gamelabel = QLabel('Select Game...')
        grid.addWidget(gamelabel, 0, 0, 1, 1)

        self.game = QComboBox()
        self.gamelist = self.db.getGameList()
        self.game.addItems(self.gamelist)
        self.game.currentIndexChanged.connect(self.gameselected)
        grid.addWidget(self.game, 0, 1, 1, 2)

        self.gamenew = QLineEdit()
        grid.addWidget(self.gamenew, 0, 3, 1, 2)

        versionlabel = QLabel('Select Version...')
        grid.addWidget(versionlabel, 1, 0, 1, 1)

        self.version = QComboBox()
        self.gamelist = []
        self.version.currentIndexChanged.connect(self.versionselected)
        grid.addWidget(self.version, 1, 1, 1, 2)

        self.versionnew = QLineEdit()
        grid.addWidget(self.versionnew, 1, 3, 1, 2)

        rbklabel = QLabel('New Rulebook Name...')
        grid.addWidget(rbklabel, 2, 0, 1, 1)

        self.rbkname = QLineEdit()
        grid.addWidget(self.rbkname, 2, 1, 1, 4)

        self.core = QCheckBox('Core', self)
        self.core.toggle()
        self.core.setEnabled(False)
        self.core.stateChanged.connect(self.coreToggle)
        grid.addWidget(self.core, 3, 0, 1, 1)

        self.corerbk = QComboBox()
        self.corerbk.hide()
        grid.addWidget(self.corerbk, 3, 1, 1, 4)

        self.buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        self.buttons.accepted.connect(self.onOk)
        self.buttons.rejected.connect(self.onCancel)
        grid.addWidget(self.buttons, 4, 0, 1, 4)

        self.setLayout(grid)

    @pyqtSlot()
    def gameselected(self):
        self.version.clear()
        gamename = self.game.currentText()

        if(gamename == 'New Game...'):
            self.gamenew.show()
            self.versionnew.show()
            self.core.setChecked(True)
            self.core.setEnabled(False)
            self.corerbk.hide()
        else:
            self.gamenew.hide()
            if not(self.version == 'New Version...'):
                self.versionnew.hide()
            self.core.setEnabled(True)
            versions = self.db.getVersionList(gamename)
            self.version.addItems(versions)


    @pyqtSlot()
    def versionselected(self):
        gamename = self.game.currentText()
        version = self.version.currentText()

        if (self.version.currentText() == 'New Version...') or \
           (not self.version.currentText()):
            self.versionnew.show()
        else:
            self.versionnew.hide()

        self.corerbk.clear()
        corebooks = self.db.getCoreRulebooksList(gamename, version)
        self.corerbk.addItems(corebooks)


    def coreToggle(self, state):
        if state == Qt.Checked:
            self.corerbk.hide()
        else:
            self.corerbk.show()


    @pyqtSlot()
    def onOk(self):
        if(self.gamenew.isVisible()):
            g = self.gamenew.text()
        else:
            g = self.game.currentText()

        if(self.versionnew.isVisible()):
            v = self.versionnew.text()
        else:
            v = self.version.currentText()

        if(self.core.isChecked()):
            c = 'True'
        else:
            c = 'False'

        rbkfilename = 'Rulebooks/{0}/{1}/{2}.rbk'.format(g, v, self.rbkname.text())
        if(os.path.exists(rbkfilename)):
            confirm = QMessageBox.question(self, 'Overwrite Rulebook?', 'There is already a rulebook with this name.  Continuing will delete the currently existing rulebook and create a new rulebook.  Are you sure you wish to continue?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
            if(confirm == QMessageBox.Yes):
                os.remove(rbkfilename)
                self.newed.emit(g, v, self.rbkname.text(), c, self.corerbk.currentText())
        else:
            self.newed.emit(g, v, self.rbkname.text(), c, self.corerbk.currentText())
        self.close()


    @pyqtSlot()
    def onCancel(self):
        self.close()
